﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using BLL;

namespace QLNhanKhau
{
    public partial class UCTachKhau : UserControl
    {
        public UCTachKhau()
        {
            InitializeComponent();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            //luu so ho khau
            SoHoKhauPublic shk = new SoHoKhauPublic();
            shk.MaSHK = DungChung.LoadMaSHK();
            shk.SoSoHoKhau = int.Parse(txtSoSHKMoi.Text);
            shk.SoNha = txtSoNhaMoi.Text;
            shk.Duong = txtDuongMoi.Text;
            shk.KhuPho = byte.Parse(txtApMoi.Text);

            shk.NguoiCap = (int)cmbNguoiCapNK.SelectedValue;
            shk.CanBoDangKi = frmDangNhap.user_login.MaND;
            shk.NgayCap = dateNgayNhapNKMoi.Value;
            shk.ChucVuNguoiCap = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV;
            SoHoKhauBLL.InsertSoHoKhau(shk);
            //update mashk
            //NhanKhauBLL.UpdateMaSHK()

        }
        public int _MaSHKCu;
        private void btnTimSHKCu_Click(object sender, EventArgs e)
        {
            if (txtSoSHK.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int SoSHK = int.Parse(txtSoSHK.Text);
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                txtDuong.Text = shk.Duong;
                txtAp.Text = shk.KhuPho.ToString();
                txtSoNha.Text = shk.SoNha;
                btnChuyenDi.Enabled = false;
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);

                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
                //txtChuHo.Text = list_nk.Where(item => item.QuanHe == "Chủ hộ").FirstOrDefault().HoTen;

                dgvNhanKhau.DataSource = list_nk;
                //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                //dgvNhanKhau.Columns[8].Visible = false;
                dgvNhanKhau.Columns[9].Visible = false;
                //dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                dgvNhanKhau.Columns[15].Visible = false;
                //dgvNhanKhau.Columns[16].Visible = false;
                dgvNhanKhau.Columns[17].Visible = false;
                dgvNhanKhau.Columns[18].Visible = false;
                dgvNhanKhau.Columns[19].Visible = false;
                dgvNhanKhau.Columns[20].Visible = false;
                dgvNhanKhau.Columns[21].Visible = false;
                dgvNhanKhau.Columns[22].Visible = false;
                dgvNhanKhau.Columns[23].Visible = false;

                //cmbCanBoDangKiCu.Text = NguoiDungBLL.LoadNguoiDungByMaND(shk.CanBoDangKi).HoTen;
                //cmbNguoiCapCu.Text = NguoiDungBLL.LoadNguoiDungByMaND(shk.NguoiCap).HoTen;
                //dateNgayCapSHKCu.Value = (DateTime)shk.NgayCap;
                //txtChucVuCu.Text = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND(shk.NguoiCap).MaCV).TenChucVu;
                //cmbChucVuCu.Text=ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND(shk.NguoiCap).MaCV).TenChucVu;
                _MaSHKCu = shk.MaSHK;
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.ForeColor = Color.AliceBlue;
                style.BackColor = Color.Red;
                for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
                {
                    if ((byte)dgvNhanKhau.Rows[i].Cells[21].Value != 0)
                    {
                        dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                        //groupBox1.Enabled = false;
                        btnChuyenDi.Enabled = false;
                        //btnSua.Enabled = false;
                        //return;
                    }
                    else
                    {
                       // groupBox1.Enabled = true;
                        btnChuyenDi.Enabled = true;
                        //btnSua.Enabled = true;
                    }
                }

            }

        }

        private void UCTachKhau_Load(object sender, EventArgs e)
        {
            txtSoSHKMoi.Text = DungChung.LoadMaSHK().ToString();
            //load cho nhân khẩu
            cmbNguoiCapNK.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiCapNK.DisplayMember = "HoTen";
            cmbNguoiCapNK.ValueMember = "MaND";
            //cmbChucVuNK.DataSource = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV);
            //cmbChucVuNK.DisplayMember = "TenChucVu";
            //cmbChucVuNK.ValueMember = "MaCV";

            txtChucVuNK.Text = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV).TenChucVu;
            dateNgayNhapNKMoi.Value = DateTime.Now;
            //txtNgayCapMoi.Text = DateTime.Now.Day.ToString();
            cmbCanBoDangKiNK.Text = frmDangNhap.user_login.HoTen;

            //load cho sổ hộ khẩu
            //cmbCanBoDangKiSHKMoi.Text = frmDangNhap.user_login.HoTen;
            //cmbNguoiCapSHKMoi.DataSource = NguoiDungBLL.LoadNguoiCap();
            //cmbNguoiCapSHKMoi.DisplayMember = "HoTen";
            //cmbNguoiCapSHKMoi.ValueMember = "MaND";
            //txtChucVuMoi.Text = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue).MaCV).TenChucVu;
            //dateNgayCap.Value = DateTime.Now;

            //load số chuyển khẩu
            txtSoChuyenKhau.Text = DungChung.SoChuyenKhau().ToString();
            txtLyDoChuyen.Text = "Tách lập hộ mới";
            txtLyDoChuyen.Enabled = false;
            txtNoiChuyen.Text = DungChung.LoadMaSHK().ToString();

        }

        private void txtSoSHKMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnChuyenDi_Click(object sender, EventArgs e)
        {
            if (txtApMoi.Text == ""  )
            {
                MessageBox.Show("Vui lòng nhập khu phố/ấp", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtApMoi.Focus();
                return;
            }
            if (txtSoNhaMoi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập số nhà", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSoNhaMoi.Focus();
                return;
            }
            if( txtDuongMoi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập tên đường", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDuongMoi.Focus();
                return;
            }
            if (NhanKhauBLL.LoadNhanKhauByMaNK(_MaNKChuyen).MaQH == 1)
            {
                MessageBox.Show("Không thể chuyển chủ hộ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SoHoKhauPublic shk = new SoHoKhauPublic();
            shk.MaSHK = DungChung.LoadMaSHK();
            shk.SoSoHoKhau = int.Parse(txtSoSHKMoi.Text);
            shk.SoNha = txtSoNhaMoi.Text;
            shk.Duong = txtDuongMoi.Text;
            shk.KhuPho = byte.Parse(txtApMoi.Text);

            shk.NguoiCap = (int)cmbNguoiCapNK.SelectedValue;
            shk.CanBoDangKi = frmDangNhap.user_login.MaND;
            shk.NgayCap = dateNgayNhapNKMoi.Value;
            shk.ChucVuNguoiCap = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV;
            SoHoKhauBLL.InsertSoHoKhau(shk);


            ChuyenKhauPublic ck = new ChuyenKhauPublic();
            ck.MaNK = _MaNKChuyen;
            ck.MaCK = DungChung.LoadMaCK();
            ck.SoChuyenKhau = DungChung.SoChuyenKhau();
            ck.LyDoChuyen = txtLyDoChuyen.Text;
            ck.NoiChuyen = shk.MaSHK;
            ck.NgayNhan = dateNgayNhapNKMoi.Value;
            ck.NgayCap = dateNgayNhapNKMoi.Value;
            ck.CanBoNoiCap = cmbCanBoDangKiNK.Text;
            ck.CanBoNoiNhan = cmbCanBoDangKiNK.Text;
            ck.NguoiCap = cmbNguoiCapNK.Text;
            ck.NguoiNhan = cmbNguoiCapNK.Text;
            ck.ChucVuNguoiCap = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV;
            ck.ChucVuNguoiNhan = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV;
            ck.NoiCap = ThietLapBLL.LoadMaDiaChi();
            ck.ThuongTru = NhanKhauBLL.LoadNhanKhauByMaNK(_MaNKChuyen).MaSHK.ToString();

            if (cmbQuanHe.Enabled == true)
                ck.MaQH = (byte)cmbQuanHe.SelectedValue;
            else
                ck.MaQH = 1;
            ChuyenKhauBLL.InsertChuyenKhau(ck);
            NhanKhauBLL.UpdateMaSHK(shk.MaSHK, _MaNKChuyen);
            NhanKhauBLL.UpdateQuanHe(_MaNKChuyen, ck.MaQH);  
            MessageBox.Show("Tách khẩu thành công","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            
            btnTimSHKCu_Click(sender, e);
            dgvNhanKhau.DataSource = NhanKhauBLL.LoadNhanKhauByMaSHK(_MaSHKCu);
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.ForeColor = Color.AliceBlue;
            style.BackColor = Color.Red;
            for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
            {
                if ((byte)dgvNhanKhau.Rows[i].Cells[21].Value != 0)
                {
                    //MessageBox.Show("geg");
                    dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                }
                //if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value==true)
                //{
                //  dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                //}
            }
            List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
            txtChuHoMoi.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
            NhanKhauBLL.UpdateChuHo(ck.MaCK, txtChuHoMoi.Text);
            dgvNhanKhauMoi.DataSource = list_nk;
            //dgvNhanKhauMoi.Columns[0].HeaderText = "Mã nhân khẩu";
            dgvNhanKhauMoi.Columns[1].HeaderText = "Họ và tên";
            //dgvNhanKhauMoi.Columns[3].HeaderText = "Tên gọi khác";
            dgvNhanKhauMoi.Columns[4].HeaderText = "CMND";
            dgvNhanKhauMoi.Columns[5].HeaderText = "Ngày sinh";
            //dgvNhanKhauMoi.Columns[6].HeaderText = "Nơi sinh";
            dgvNhanKhauMoi.Columns[7].HeaderText = "Giới tính";
            dgvNhanKhauMoi.Columns[8].HeaderText = "Quê quán";
            dgvNhanKhauMoi.Columns[9].HeaderText = "Dân tộc";
            dgvNhanKhauMoi.Columns[10].HeaderText = "Nghề nghiệp";
            dgvNhanKhauMoi.Columns[11].HeaderText = "Quốc tịch";
            //dgvNhanKhauMoi.Columns[12].HeaderText = "Chứng tử";
            dgvNhanKhauMoi.Columns[13].HeaderText = "Tôn giáo";
            dgvNhanKhauMoi.Columns[14].HeaderText = "Trình độ";
            //dgvNhanKhauMoi.Columns[15].HeaderText = "Sổ hộ khẩu";
            dgvNhanKhauMoi.Columns[16].HeaderText = "Quan hệ";
            dgvNhanKhauMoi.Columns[0].Visible = false;
            //dgvNhanKhauMoi.Columns[1].Visible = false;
            dgvNhanKhauMoi.Columns[2].Visible = false;
            dgvNhanKhauMoi.Columns[3].Visible = false;
            //dgvNhanKhauMoi.Columns[4].Visible = false;
            //dgvNhanKhauMoi.Columns[5].Visible = false;
            dgvNhanKhauMoi.Columns[6].Visible = false;
            //dgvNhanKhauMoi.Columns[7].Visible = false;
            //dgvNhanKhauMoi.Columns[8].Visible = false;
            dgvNhanKhauMoi.Columns[9].Visible = false;
            //dgvNhanKhauMoi.Columns[10].Visible = false;
            dgvNhanKhauMoi.Columns[11].Visible = false;
            dgvNhanKhauMoi.Columns[12].Visible = false;
            dgvNhanKhauMoi.Columns[13].Visible = false;
            dgvNhanKhauMoi.Columns[14].Visible = false;
            dgvNhanKhauMoi.Columns[15].Visible = false;
            //dgvNhanKhauMoi.Columns[16].Visible = false;
            dgvNhanKhauMoi.Columns[17].Visible = false;
            dgvNhanKhauMoi.Columns[18].Visible = false;
            dgvNhanKhauMoi.Columns[19].Visible = false;
            dgvNhanKhauMoi.Columns[20].Visible = false;
            dgvNhanKhauMoi.Columns[21].Visible = false;
            dgvNhanKhauMoi.Columns[22].Visible = false;
            dgvNhanKhauMoi.Columns[23].Visible = false;
            cmbQuanHe.Enabled = true;
            cmbQuanHe.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHe.DisplayMember = "TenQuanHe";
            cmbQuanHe.ValueMember = "MaQH";
            
            style.ForeColor = Color.AliceBlue;
            style.BackColor = Color.Red;
            for (int i = dgvNhanKhauMoi.RowCount - 1; i >= 0; i--)
            {
                if ((byte)dgvNhanKhauMoi.Rows[i].Cells[21].Value != 0)
                {
                    //MessageBox.Show("geg");
                    dgvNhanKhauMoi.Rows[i].DefaultCellStyle = style;
                }
                //if ((bool)dgvNguoiDung.Rows[i].Cells[5].Value==true)
                //{
                //  dgvNguoiDung.Rows[i].DefaultCellStyle = style;
                //}
            }

        }


        //private void cmbNguoiCapSHKMoi_SelectionChangeCommitted(object sender, EventArgs e)
        //{
        //    //cmbChucVuMoi.DataSource = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue).MaCV);
        //    //cmbChucVuMoi.DisplayMember = "TenChucVu";
        //    //cmbChucVuMoi.ValueMember = "MaCV";
        //    NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapSHKMoi.SelectedValue);
        //    ////.Text = nd.ChucVu;
        //    txtChucVuMoi.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
        //    txtChucVuMoi.Enabled = false;
        //}

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 8)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTimSHKCu_Click(sender, e);
        }

        private void txtSoChuyenKhau_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 8)
            {
                e.Handled = true;
            }
        }
        public int _MaNKChuyen;
        private void dgvNhanKhau_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvNhanKhau.SelectedRows.Count > 0)
            {
                btnChuyenDi.Enabled = true;
                _MaNKChuyen = (int)dgvNhanKhau.SelectedRows[0].Cells[0].Value;
            }
            else
                btnChuyenDi.Enabled = false;

            if (dgvNhanKhau.SelectedRows.Count > 0)
            {
                if ((byte)dgvNhanKhau.SelectedRows[0].Cells[21].Value != 0 || (byte)dgvNhanKhau.SelectedRows[0].Cells[16].Value ==1)
                {
                    btnChuyenDi.Enabled = false;
                }
                else
                    btnChuyenDi.Enabled = true;
            }
        }

        private void cmbNguoiCapNK_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //cmbChucVuNK.DataSource = ChucVuBLL.LoadChucVuByMaCV((int)NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue).MaCV);
            //cmbChucVuNK.DisplayMember = "TenChucVu";
            //cmbChucVuNK.ValueMember = "MaCV";
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCapNK.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNK.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNK.Enabled = false;
        }

        private void txtSoNhaMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtSoNhaMoi.Text.Length > 5 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void txtDuongMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtDuongMoi.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtApMoi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtApMoi.Text.Length > 1 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

    
    }
}
